App({
  data: {
    // 图片公用的src
    img_gy_src: "/imgs",
    // img_gy_src: "http://www.yifale.com/static/imgs",
    qu_yu_list: "",

    tab_bar: [{
      "selectedIconPath": "/imgs/tab_bar/index_active.png",
      "iconPath": "/imgs/tab_bar/index.png",
    }, {
      "selectedIconPath": "imgs/tab_bar/business_active.png",
      "iconPath": "imgs/tab_bar/business.png",
    }, {
      "selectedIconPath": "imgs/tab_bar/snapUp_active.png",
      "iconPath": "imgs/tab_bar/snapUp.png",
    }, {
      "selectedIconPath": "imgs/tab_bar/vouchers_active.png",
      "iconPath": "imgs/tab_bar/vouchers.png",
    }, {
      "selectedIconPath": "imgs/tab_bar/mine_active.png",
      "iconPath": "imgs/tab_bar/mine.png",
    }],
  },
  onLaunch: function() {
    // 设置tab_bar
    this.tab_bar();
    // 获取是否隐藏
    this.hide();

    //获取地理位置
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude

        that.globalData.latitude = latitude
        that.globalData.longitude = longitude
      },
      fail: function (res) {
        wx.authorize({
          scope: 'scope.userLocation',
        })
      }
    })

    var that = this;
    const updateManager = wx.getUpdateManager();

    updateManager.onCheckForUpdate(function(res) {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function() {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function() {
      // 新的版本下载失败
    })
  },
  globalData: {
    userinfo: wx.getStorageSync('userinfo'),
    token: wx.getStorageSync('token'),
  },
  // 设置tab_bar
  tab_bar() {
    // 获取数据
    wx.request({
      url: "https://www.yifale.com/api/set",
      header: {
        'Accept': 'application/json',
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: (res) => {
        var tab_bar = this.data.tab_bar;
        var text_data = res.data;
        tab_bar.forEach((obj, index) => {
          wx.setTabBarItem({
            index: index,
            text: text_data[index],
            iconPath: obj.iconPath,
            selectedIconPath: obj.selectedIconPath
          })
        })
      },
    })
  },
  // 获取是否隐藏
  hide() {
    // 获取数据
    wx.request({
      url: "https://www.yifale.com/api/set/debug",
      header: {
        'Accept': 'application/json',
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: (res) => {
        this.data.hide = res.data
      },
    })
  },
  // 设置监听器
  setWatcher(data, watch) { // 接收index.js传过来的data对象和watch对象
    Object.keys(watch).forEach(v => { // 将watch对象内的key遍历
      this.observe(data, v, watch[v]); // 监听data内的v属性，传入watch内对应函数以调用
    })
  },

  // 监听属性 并执行监听函数
  observe(obj, key, watchFun) {
    var val = obj[key]; // 给该属性设默认值
    Object.defineProperty(obj, key, {
      configurable: true,
      enumerable: true,
      set: function(value) {
        val = value;
        watchFun(value, val); // 赋值(set)时，调用对应函数
      },
      get: function() {
        return val;
      }
    })
  }
})